package cn.com.egova.test.picture;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class urlTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException, MalformedURLException {

        long startTime = System.currentTimeMillis();   //获取开始时间

        String URL = "jdbc:mysql://222.128.14.106:10272/xclife?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
        String USER = "root";
        String PASSWORD = "xczg@2019";
        Class.forName("com.mysql.jdbc.Driver");
        Connection dbConn = DriverManager.getConnection(URL, USER, PASSWORD);
        Statement st = dbConn.createStatement();
        ResultSet rs = st.executeQuery("select * from picture where real_name is null or real_name = '' limit 1, 4");
        //ResultSet rs=st.executeQuery("select * from url where id > 1252 and id < 1283");

        // 封装查询结果集
        Map<String, String> urlMap = new HashMap<String, String>();
        while (rs.next()) {
            urlMap.put(rs.getString("id"), rs.getString("url"));
        }

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        
        int index = 0;
        
        String sql = "update picture set real_name = ? where id = ?";
        PreparedStatement prest = dbConn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE); 

        for (Map.Entry<String, String> entry : urlMap.entrySet()) {
            String filename = "";
            String filePath = entry.getValue();
            boolean isok = false;

            // 从UrlConnection中获取文件名称
            URL sourcPicUrl = null;
            try {
            	sourcPicUrl = new URL(filePath);
            	URLConnection connention = sourcPicUrl.openConnection();
                Map<String, List<String>> hf = connention.getHeaderFields();
                Set<String> key = hf.keySet();
                for (String skey : key) {
                    List<String> values = hf.get(skey);
                    for (String value : values) {
                        String result;
                        try {
                            result = new String(value.getBytes("utf-8"), "gbk");
                            int location = result.indexOf("filename");
                            if (location >= 0) {
                                result = result.substring(location
                                        + "filename".length());
                                filename = result
                                        .substring(result.indexOf("=") + 1);
                                isok = true;
                                String chiStr = null;
                                try {
                                    filename = filename.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
                                    filename = filename.replace("\"", "");
                                    chiStr = URLDecoder.decode(filename, "utf-8");
                                } catch (UnsupportedEncodingException e) {
                                    e.printStackTrace();
                                }
                                filename = chiStr.substring(chiStr.lastIndexOf("'") + 1);
                            }
                        } catch (UnsupportedEncodingException e) {
                            System.out.println("文件名转换失败");
                            e.printStackTrace();
                        }
                    }
                    if (isok) {
                        break;
                    }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 从路径中获取文件名
            if (filename == null || "".equals(filename)) {
                filename = filePath.substring(filePath.lastIndexOf("/") + 1);
            }

           // URL url = new URL(filePath);
            if (null != filePath) {
            	index++;
            	System.out.println(index + ":" + filePath);
            	
            	URL url = new URL(filePath);
                if (null != filePath) {
	                try {
	                    //写文件到磁盘
	                	
	                    HttpURLConnection connention = (HttpURLConnection) url.openConnection();
	                    //HttpURLConnection connention = (HttpURLConnection) conn;
	                	connention.setRequestMethod("GET");
	                	connention.connect();
	                    InputStream is = connention.getInputStream();
	                    bis = new BufferedInputStream(is);
	
	                    File file = new File("E:/test/download2/" + filePath.substring(filePath.lastIndexOf("=") + 1) + filename.substring(filename.lastIndexOf(".")));
	                    if (file.exists()) {
	                        System.out.println("文件名：" + filePath.substring(filePath.lastIndexOf("=") + 1) + "已存在");
	                    }
	                    FileOutputStream fos = new FileOutputStream(file);
	                    bos = new BufferedOutputStream(fos);
	                    int b = 0;
	                    byte[] byArr = new byte[1024 * 4];
	                    while ((b = bis.read(byArr)) != -1) {
	                        bos.write(byArr, 0, b);
	                    }
	                    
	                    connention.disconnect();
	                    
	                    //将文件名同步到数据表
	                    sql = "update picture set real_name='" + filename + "' where id='" + entry.getKey() + "'";
	                    dbConn.prepareStatement(sql).executeUpdate();
	                    
	                    prest.setObject(1, filename);
	                    prest.setObject(2, entry.getKey());
	                    prest.addBatch();
	                    
	                    if (index % 3000 == 0) {
				        	prest.executeBatch();
				        	dbConn.commit();
				        }
	                    
	                } catch (Exception e) {
	                    e.printStackTrace();
	                    System.out.println("文件保存失败" + index + ":" + filePath);
	                } finally {
	                    try {
	                        if (bis != null) {
	                            bis.close();
	                        }
	                        if (bos != null) {
	                            bos.close();
	                        }
	                        
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
                }
            }
        }
        
        if (index % 3000 > 0) {
        	prest.executeBatch();
        	dbConn.commit();
        }
        
        rs.close();
        st.close();
        dbConn.close();

        long endTime = System.currentTimeMillis(); //获取结束时间

        System.out.println("程序运行时间： " + (endTime - startTime) + "ms");
    }
}

